package net.yb.dao.impl;

import net.yb.dao.CategoryDao;
import net.yb.entity.Category;
import net.yb.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 分类Dao实现类
 */
public class CategoryDaoImpl implements CategoryDao {
    @Override
    public Category selectById(Integer cid) {
        try {
            Connection conn = JdbcUtil.getConnection();
            String sql = "select cid, cname, info from category where cid = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, cid);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                Category category = new Category();
                category.setCid(rs.getInt("cid"));
                category.setCname(rs.getString("cname"));
                category.setInfo(rs.getString("info"));
                return category;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
